<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/7
 * Time: 9:32
 */
namespace app\v1\model;

use app\v1\logic\MyFunctions;
use think\Db;

class Fightgroups extends MyFunctions
{
    private $tableName = "fightgroups";//表名

    /*
     * 创建团购
     * @param string merchantId | 商户Id
     * @param int  people | 团购人数
     * @param float amount | 金额
     * @param string userId | 用户id
     * @param string goodId | 商品Id
     * */
    public function add($data)
    {
        $isTrue = Db::name($this->tableName)->where("goodId", $data['goodId'])->where("status", "0")->find();
        if (!empty($data['merchantId']) && !empty($data['groupNum']) && is_numeric($data['groupNum']) && !empty($data['goodId']) && !$isTrue) {
            $data['createTime'] = time();
            $data['status'] = "0";
            $data['remarks'] = "";
            $data['payTime'] = "";
            $data['isDelete'] = "0";//伪删除
            $data['surplusPeople'] = $data['groupNum'];
            $data2 = ["data" => ["fightgroupId" => Db::name($this->tableName)->insertGetId($data), "groupPrice" => $data["groupPrice"] * 100]];
            //更新商品团购ID
            return $this->getApiByName(config("goodDomain") . "/goods/_id?_id=" . $data['goodId'], "PUT", $data2) ? 1 : 0;
        } else {
            return 404;//字段填写不完整或该商品已存在团购
        }
    }

    /*
     * 更新团购状态
     * */
    public function updateFG($data)
    {
        if (!empty($data['data'])) {
            $_id = !empty($data['_id']) ? $data['_id'] : $data['goodId'];
            $str = !empty($data['_id']) ? "_id" : "goodId";
            return Db::name($this->tableName)->where($str, $_id)->where("isDelete", "0")->update($data['data']) ? 200 : 404;
        } else if (!empty($data['_id'])) {
            //根据团购ID修改
            $res = Db::name($this->tableName)->where("_id", $data['_id'])->where("isDelete", "0")->setDec("surplusPeople") ? 200 : 404;
            $arr=Db::name($this->tableName)->where("_id", $data['_id'])->field("surplusPeople,merchantId,goodId")->find();
            $merchantInfo=json_decode($this->getApiByName(config("merchantDomain")."/merchant/merchant/_id/".$arr['merchantId']));
            $goodInfo=json_decode($this->getApiByName(config("goodDomain")."goods/ids?_ids=".$arr['goodId']."&type=1"));
            if ((int)$arr["surplusPeople"] < 1) {
                $res = Db::name($this->tableName)->where("_id", $data['_id'])->update(['status' => 1, "isDelete" => 1]) ? 201 : 201;//拼团成功
                $data=[
                    "merchantId"=>$arr['merchantId'],
                    "phone"=>$merchantInfo->tel,
                    "goodName"=>"团购商品".$goodInfo['brands']
                ];
                $data2=[
                    "data"=>['groupPrice'=>"0","groupNum"=>0,"joinGroup"=>false,"endTime"=>0,"fightgroupId"=>0]
                ];
                $this->getApiByName(config("merchantDomain")."/merchant/sendSms/","POST",$data);//发送拼团成功短信
                $this->getApiByName(config("goodDomain")."/goods/_id?_id=".$arr['goodId'],"PUT",$data2);//标记商品团购已完成
            }
            return $res;
        } else if (!empty($data['goodId'])) {
            //根据商品Idxiugai
            if (Db::name($this->tableName)->where("goodId", $data['goodId'])->where("isDelete", "0")->find()) {
                return Db::name($this->tableName)->where("goodId", $data['goodId'])->update(['status' => $data['status']]) ? 200 : 404;
            } else {
                return 304;
            }
        } else {
            return 404;
        }
    }

    /*
     * 删除团购订单
     * */
    public function del($id)
    {
        return Db::name($this->tableName)->where("_id", $id)->update(['isDelete' => 1]);
    }

    /*
     * 根据Id或商品Id获取团购信息
     * */
    public function getFG($id)
    {
        $str = !empty($id['goodId']) ? "goodId" : "_id";
        $id2 = !empty($id['goodId']) ? $id['goodId'] : $id;
        $FGInfo = Db::name($this->tableName)->where($str, $id2)->where("isDelete", "0")->find();

        if (!empty($id['goodId'])&&is_array($FGInfo)) {
            $goodInfo = json_decode($this->getApiByName(config("goodDomain") . "/goods/ids?_ids=" . $id['goodId'] . "&type=0", "get"));

            $FGInfo['retailPrice'] = $goodInfo[0]->retailPrice;

        }
        return is_array($FGInfo) ? $FGInfo : 404;


    }

    /*
     * 根据状态获取团购信息
     * */
    public function getFGByStatus($data)
    {
        $data['page'] = empty($data['page']) ? 1 : $data['page'];
        $data["total"] = $data["total"] ? $data["total"] : 20;
        $status = $data['status'];
        return Db::name($this->tableName)->where("status", "$status")->limit(($data['page'] - 1) * $data["total"], $data["total"])->select();
    }

    /*
     * 更新订单
     * */
    public function putBusinesspay($data)
    {
        if ($data['orderStatus'] == 2) {

        } else {
            return Db::name($this->tableName)->update($data);
        }
    }

    /*
  * 根据支付订单原路返回金额给商户
  * @param
  *
  * */
    public function returnMoneyToUser()
    {

    }

}